package com.office.newUI.components.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.office.anywher.R;

public class UIUtils {
	public static View getTableView(Context context,Map<String,String> map,OnClickListener click){
		List<RowBean> mList = new ArrayList<RowBean>();
		for (Map.Entry<String, String> entry : map.entrySet()) {
			RowBean aRowBean = new RowBean(entry.getKey(),entry.getValue());
			mList.add(aRowBean);
		}
		return getTableView(context,mList,click);
	}
	/**
	 * 
	 * @param context 
	 * @param mList shwo's datas
	 * @param click row's click envent
	 * @return
	 */
	public static View getTableView(Context context,List<RowBean> mList,OnClickListener click){
		Collections.sort(mList);
		DisplayMetrics aDisplayMetrics = context.getResources().getDisplayMetrics();
		int aScreenWidth = aDisplayMetrics.widthPixels;
		int aRowWidth = aScreenWidth - 20;
		LayoutInflater aInflater = LayoutInflater.from(context);
        LinearLayout aLinearLayout = (LinearLayout) aInflater.inflate(R.layout.table_layout, null);
        TableLayout aTableLayout = (TableLayout)aLinearLayout.findViewById(R.id.table_id);
        Resources aResources = context.getResources();
        for(int i=0;i<mList.size();i++){
        	TableRow aTableRow = new TableRow(context);
        	aTableRow.setBackgroundColor(Color.parseColor("#ffffffff"));
        	if(click!=null)aTableRow.setOnClickListener(click);
        	android.widget.TableRow.LayoutParams aTableRowLp = new android.widget.TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        	aTableRow.setLayoutParams(aTableRowLp);
        	if(mList.size() == 1){ 
        		aTableRow.setBackgroundDrawable(aResources.getDrawable(R.drawable.shape_bottom_top_corner_line));
        	}else if(i==0){
        		aTableRow.setBackgroundDrawable(aResources.getDrawable(R.drawable.shape_top_corner_no_bottom_line));
        	}else if(i == mList.size()-1){
        		aTableRow.setBackgroundDrawable(aResources.getDrawable(R.drawable.shape_bottom_corner_no_top_line));
        	}else{
        		aTableRow.setBackgroundDrawable(aResources.getDrawable(R.drawable.shape_no_corner_without_bottom));
        	}
        	int keyWidth = 88;
        	aTableRow.setPadding(10, 10, 10, 10);
        	TextView aKeyTextView = new TextView(context);
        	aKeyTextView.setWidth(keyWidth);
        	aKeyTextView.setGravity(Gravity.CENTER_VERTICAL);
        	aKeyTextView.setPadding(0, 0, 10, 0);
        	aKeyTextView.setText(mList.get(i).key+":");
        	aTableRow.addView(aKeyTextView);
        	TextView aValueTextView = new TextView(context);
        	aValueTextView.setWidth(aRowWidth - keyWidth -20);
        	aValueTextView.setGravity(Gravity.CENTER_VERTICAL);
        	aValueTextView.setPadding(0, 0, 10, 0);
        	aValueTextView.setText(mList.get(i).value);
        	aTableRow.addView(aValueTextView,aTableRowLp);
        	aTableLayout.addView(aTableRow/*,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)*/);
        }
		return aLinearLayout;
	}
}
